package com.zhou.service.impL;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.domain.Dto.PageDto;
import com.zhou.domain.FlightPath;
import com.zhou.listener.RedisPublish;
import com.zhou.mapper.FlightPathMapper;
import com.zhou.service.FlightPathService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.NoStackLogException;
import zhongbo.java.air.common.vo.PageVo;

@Service
public class FlightPathServiceImpl implements FlightPathService {

    @Autowired
    private FlightPathMapper flightPathMapper;

    @Autowired
    private RedisPublish redisPublish;

    //新增
    public void add(FlightPath flightPath){
        flightPathMapper.insert(flightPath);
        redisPublish.push("新增数据"+flightPath.toString());
    }

    //删除
    public void deleteById(String id){
        if(id==null||id.isEmpty()){
            throw new NoStackLogException("id不能为空");
        }
        LambdaQueryWrapper<FlightPath> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FlightPath::getId,id);
        flightPathMapper.delete(lqw);
        redisPublish.push("删除"+id);
    }

    //修改
    public void update(FlightPath flightPath){
        LambdaQueryWrapper<FlightPath> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FlightPath::getId,flightPath.getId());
        flightPathMapper.update(flightPath,lqw);
        redisPublish.push("修改"+flightPath.toString());
    }

    //分页查询，按字段排序
    public PageVo<FlightPath> selectPage(PageDto airCraftPageDto){
        Page<FlightPath> page = new Page<>(airCraftPageDto.getPageNum(),airCraftPageDto.getPageSize());
        QueryWrapper<FlightPath> qw = new QueryWrapper<>();
        qw.orderBy(true,airCraftPageDto.isAsc(),airCraftPageDto.getSortField());
        Page<FlightPath> flightPathPage = flightPathMapper.selectPage(page, qw);
        PageVo<FlightPath> flightPathPageVo = new PageVo<>();
        flightPathPageVo.setPage(flightPathPage.getPages());
        flightPathPageVo.setPageSize(flightPathPage.getSize());
        flightPathPageVo.setPageTotal(flightPathPage.getTotal());
        flightPathPageVo.setRecords(flightPathPage.getRecords());
        redisPublish.push("查询结果"+flightPathPageVo.toString());
        return flightPathPageVo;
    }

}
